home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 1.0 beta / flock-1.0RC3.en-US.win32.exe / flock / components / flockSearchAPIYahoo.js < prev    next >
Text File  |  2007-10-18  |  10KB  |  253 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const Cc = Components.classes;
  20. const Ci = Components.interfaces;
  21. const Cr = Components.results;
  22.  
  23. const YAHOO_SEARCH_CID         = Components.ID('{88a8b490-d044-4a8b-bb03-1798864ffabf}');
  24.  
  25. const nsISupports                   = Components.interfaces.nsISupports;
  26. const nsIClassInfo                  = Components.interfaces.nsIClassInfo;
  27. const nsIFactory                    = Components.interfaces.nsIFactory;
  28. const nsIProperties                 = Components.interfaces.nsIProperties;
  29. const nsILocalFile                  = Components.interfaces.nsILocalFile;
  30. const nsIFile                       = Components.interfaces.nsIFile;
  31. const nsIIOService                  = Components.interfaces.nsIIOService;
  32. const nsIFileProtocolHandler        = Components.interfaces.nsIFileProtocolHandler;
  33. const nsIRDFRemoteDataSource        = Components.interfaces.nsIRDFRemoteDataSource;
  34. const nsIRDFDataSource              = Components.interfaces.nsIRDFDataSource;
  35. const flockISearchService           = Components.interfaces.flockISearchService;
  36. const nsIXMLHttpRequest             = Components.interfaces.nsIXMLHttpRequest;
  37.  
  38. const YAHOO_SEARCH_CONTRACTID       = '@flock.com/?flock-search-yahoo;1';
  39. const DIRECTORY_SERVICE_CONTRACTID  = '@mozilla.org/file/directory_service;1';
  40. const LOCAL_FILE_CONTRACTID         = '@mozilla.org/file/local;1';
  41. const PREFERENCES_CONTRACTID        = '@mozilla.org/preferences-service;1';
  42. const IO_SERVICE_CONTRACTID         = '@mozilla.org/network/io-service;1';
  43. const XMLHTTPREQUEST_CONTRACTID     = '@mozilla.org/xmlextras/xmlhttprequest;1'
  44.  
  45. const flockIError = Components.interfaces.flockIError;
  46. const FLOCK_ERROR_CONTRACTID = '@flock.com/error;1'
  47.  
  48. const YAHOO_SEARCH_API_URL = "http://api.search.yahoo.com/WebSearchService/V1/";
  49.  
  50. function yahooSearchService() {
  51.   var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  52.   var bundle = sbs.createBundle("chrome://flock/locale/search/search.properties");
  53.   this.serviceName = bundle.GetStringFromName("flock.search.api.yahoo");
  54. }
  55.  
  56. yahooSearchService.prototype.shortName = "yahoo";
  57. yahooSearchService.prototype.icon = "chrome://browser/skin/flock/search/yahoo.ico";
  58. yahooSearchService.prototype.ref = "urn:flock:search:yahoo";
  59. yahooSearchService.prototype.fullResultsUrl = "http://search.yahoo.com/search?ei=UTF-8&fr=flo&p=";
  60.  
  61. yahooSearchService.prototype.search = function (aQuery, aNumResults, aListener, aDatasource) {
  62.     var inst = this;
  63.     
  64.     var url = YAHOO_SEARCH_API_URL + "webSearch?" 
  65.        + "appid=flock-search"
  66.        + "&query=" + encodeURIComponent(aQuery)
  67.        + "&zip=" 
  68.        + "&start=1&results=" + aNumResults
  69.        + "&fr=flock-search";
  70.  
  71.     debug(url + " < call\n");
  72.     this.req = Components.classes[XMLHTTPREQUEST_CONTRACTID].createInstance(nsIXMLHttpRequest);
  73.     this.req instanceof Components.interfaces.nsIJSXMLHttpRequest;
  74.     this.req.open('GET', url, true);
  75.     var req = this.req;    
  76.     //debug('request is ' + req + '\n');
  77.     this.req.onreadystatechange = function (aEvt) {
  78.       if(inst.req.readyState == 4) {
  79.           // debug("\nRESPONSE\n" + inst.xmlhttp.responseText);
  80.           try {
  81.               if(req.status == 200 || req.status == 201) {
  82.                  try {
  83.                      //processor(listener, inst);
  84.                      var rdf = inst.readXML(req.responseXML, aDatasource);
  85.                      var numResults = inst.getNumResults(req.responseXML);
  86.                      aListener.foundResults(numResults, rdf, inst.shortName, aQuery);
  87.                  } catch(e) {
  88.                      debug(e + " " + e.lineNumber+"\n");
  89.                  }
  90.               }
  91.               else {
  92.                   var faultString = req.responseText;
  93.                   // listener.onFault(faultString);
  94.               }
  95.           } catch(e) {
  96.               debug(e + " " + e.fileName + " " + e.lineNumber + "\n");
  97.               //listener.onError(inst.ERROR_PARSER);
  98.           }
  99.       }
  100.     }
  101.     
  102.     this.req.send(null);  
  103. }
  104.  
  105. yahooSearchService.prototype.getNumResults = function (xmlDoc) {
  106.     try {
  107.         var results = xmlDoc.getElementsByTagName("Result"); 
  108.         return results.length;
  109.     } catch (ex) {
  110.     
  111.     }
  112. }
  113.  
  114. yahooSearchService.prototype.readXML = function (xmlDoc, aDatasource) {
  115.   var unescapeHTML = Cc["@mozilla.org/feed-unescapehtml;1"]
  116.       .getService(Ci.nsIScriptableUnescapeHTML);
  117.   try {
  118.     var resultSet = xmlDoc.getElementsByTagName("ResultSet")[0];                                                              
  119.     var totalResultsAvailable = parseInt(resultSet.getAttribute("totalResultsAvailable"));  
  120.     var totalResultsReturned = parseInt(resultSet.getAttribute("totalResultsReturned"));                                      
  121.     var firstResultPosition = parseInt(resultSet.getAttribute("firstResultPosition"));                                        
  122.     var start = firstResultPosition;                                                                                          
  123.     var end = firstResultPosition + totalResultsReturned - 1;                                                                 
  124.     var results = xmlDoc.getElementsByTagName("Result"); 
  125.  
  126.     // Create an in-memory datasource
  127.     var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  128.         .getService(Components.interfaces.nsIRDFService);
  129.     var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]
  130.         .getService(Components.interfaces.nsIRDFContainerUtils);
  131.     var rootNode = rdfService.GetResource("urn:flock:search:yahoo");
  132.         
  133.     
  134.     // clear any existing results
  135.     var props=aDatasource.ArcLabelsOut(rootNode);
  136.     while(props.hasMoreElements()){
  137.         var prop=props.getNext();
  138.         var target=aDatasource.GetTarget(rootNode,prop,true);
  139.         try {
  140.           //target=target.QueryInterface(Components.interfaces.nsIRDFResource);
  141.           aDatasource.Unassert(rootNode,prop,target)
  142.         }
  143.         catch (e){
  144.             debug ('yahoo error clearing the ds ' + e + '\n');
  145.         }
  146.     }    
  147.     
  148.     rootNode = rdfService.GetResource("urn:flock:search:yahoo"); 
  149.     container = rdfContainerUtils.MakeSeq(aDatasource, rootNode);
  150.     
  151.     // Fill it with the results
  152.     for (var i = 0; i < results.length; i++) { 
  153.         var result = results[i];                                                                                                
  154.         var title = result.getElementsByTagName('Title')[0].firstChild.nodeValue;
  155.         var clickUrl = result.getElementsByTagName('ClickUrl')[0].firstChild.nodeValue;
  156.         var url = result.getElementsByTagName('Url')[0].firstChild.nodeValue;
  157.         var subject = rdfService.GetResource(url);
  158.  
  159.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#Name");
  160.         var name = rdfService.GetLiteral(unescapeHTML.unescape(title));                
  161.         aDatasource.Assert(subject, predicate, name, true);
  162.  
  163.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#URL");
  164.         var name = rdfService.GetLiteral(url);
  165.         aDatasource.Assert(subject, predicate, name, true);
  166.         
  167.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#favicon");
  168.         var name = rdfService.GetLiteral(this.icon);
  169.         aDatasource.Assert(subject, predicate, name, true);
  170.  
  171.         container.AppendElement(subject);
  172.     }
  173.  
  174.     return aDatasource;
  175.   } catch(exception) {
  176.     debug('Exception occurred while reading XML (i=' + i + '; start=' + start + ';end=' + end + '): ' + exception);
  177.   }
  178. }
  179.  
  180.  
  181. yahooSearchService.prototype.flags = nsIClassInfo.SINGLETON;
  182. yahooSearchService.prototype.classDescription = "Yahoo! Search Service";
  183. yahooSearchService.prototype.getInterfaces = function (count) {
  184.     var interfaceList = [flockISearchService, nsIClassInfo];
  185.     count.value = interfaceList.length;
  186.     return interfaceList;
  187. }
  188. yahooSearchService.prototype.getHelperForLanguage = function (count) {return null;}
  189.  
  190. // the nsISupports implementation
  191. yahooSearchService.prototype.QueryInterface =
  192. function (iid) {
  193.     if (!iid.equals(flockISearchService) && 
  194.         !iid.equals(nsIClassInfo) &&
  195.         !iid.equals(nsISupports))
  196.         throw Components.results.NS_ERROR_NO_INTERFACE;
  197.     if (iid.equals(nsIRDFDataSource) && !this.dataSourceSetup) {
  198.     }
  199.     return this;
  200. }
  201.  
  202. // Module implementation
  203. var FlockSearchModule = new Object();
  204.  
  205. FlockSearchModule.registerSelf =
  206. function (compMgr, fileSpec, location, type)
  207. {
  208.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  209.  
  210.     compMgr.registerFactoryLocation(YAHOO_SEARCH_CID, 
  211.                                     "Yahoo Search JS Component",
  212.                                     YAHOO_SEARCH_CONTRACTID, 
  213.                                     fileSpec, 
  214.                                     location,
  215.                                     type);
  216.     //necessary category registration
  217.     var catmgr = Components.classes["@mozilla.org/categorymanager;1"]
  218.         .getService (Components.interfaces.nsICategoryManager);
  219.     catmgr.addCategoryEntry('flockISearchService', 'yahoo', YAHOO_SEARCH_CONTRACTID, true, true);
  220. }
  221.  
  222. FlockSearchModule.getClassObject =
  223. function (compMgr, cid, iid) {
  224.     if (!cid.equals(YAHOO_SEARCH_CID))
  225.         throw Components.results.NS_ERROR_NO_INTERFACE;
  226.     
  227.     if (!iid.equals(Components.interfaces.nsIFactory))
  228.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  229.     
  230.     return SeachServiceFactory;
  231. }
  232.  
  233. FlockSearchModule.canUnload =
  234. function(compMgr)
  235. {
  236.     return true;
  237. }
  238.     
  239. /* factory object */
  240. var SeachServiceFactory = new Object();
  241.  
  242. SeachServiceFactory.createInstance =
  243. function (outer, iid) {
  244.     if (outer != null)
  245.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  246.     return (new yahooSearchService()).QueryInterface(iid);
  247. }
  248.  
  249. /* entrypoint */
  250. function NSGetModule(compMgr, fileSpec) {
  251.     return FlockSearchModule;
  252. }
  253.